<template>
  <el-container>
    <!-- header区域 -->
    <el-header class="homeHeader"
      ><h2>电商后台管理系统</h2>
      <el-button type="info" @click="logOut">退出</el-button></el-header
    >

    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '72px' : '200px'">
        <!-- 收起展开按钮 -->
        <div class="center">
          <el-button type="primary" @click="isCollapse = !isCollapse">{{ isCollapse == false ? '收起' : '展开' }}</el-button>
        </div>
        <!-- 侧边栏菜单 -->
        <el-menu :default-active="activePath" :router="true" :unique-opened="true" background-color="#333744" text-color="#fff" active-text-color="#409EFF" :collapse="isCollapse" :collapse-transition="false">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuInfo" :key="item.id">
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconObj[item.id]"></i>
              <!-- 文本内容 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item @click="activePathFn('/home/' + citem.path)" :index="'/home/' + citem.path" v-for="citem in item.children" :key="citem.id">
              <!-- 图标 -->
              <i class="el-icon-menu"></i>
              <!-- 文本内容 -->
              <span>{{ citem.authName }}</span>
            </el-menu-item>
          </el-submenu>
          
        </el-menu>
      </el-aside>

      <!-- main区域 -->
      <el-main>
        <!-- 路由出口 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { getLeftMenu } from '@/api/index.js'
export default {
  data() {
    return {
      menuInfo: [],
      iconObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-grid',
        101: 'el-icon-s-shop',
        102: 'el-icon-s-order',
        145: 'el-icon-s-opportunity',
      },
      isCollapse: false,
      activePath: '',
    }
  },
  created() {
    this.getLeftMenu()
    //刷新页面时重新获取当前路径
    this.activePath = sessionStorage.getItem('activepath')
  },
  methods: {
    activePathFn(activePath) {
      //会话存储当前激活路径
      sessionStorage.setItem('activepath', activePath)
      //当前激活路径
      this.activePath = activePath
    },
    logOut() {
      // remove('item')   item 为 存储的 key 名
      sessionStorage.removeItem('token')
      // 退出后跳转到登录页
      this.$router.push('/login')
      // 提示信息
      this.$message.success('退出登录成功')
    },
    async getLeftMenu() {
      let { data: res } = await getLeftMenu()
      if (res.meta.status == 200) {
        this.menuInfo = res.data
      } else {
        return this.$message.error(res.meta.msg)
      }
    },
  },
}
</script>

<style lang="less" scoped>
.center {
  text-align: center;
}
.homeHeader {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  h2 {
    font-weight: normal;
  }
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
